#include "loginviewdialog.h"
#include "core/constants.h"
#include "core/baseview.h"
#include "gui/basedialog.h"
#include "gui/collapsiblewidget.h"

LoginViewDialog::LoginViewDialog(QWidget *parent) :
    BaseDialog(parent)
{
    this->setWindowTitle("Login");
    this->setMinimumSize(300,180);
    this->initDialogComponents();

    connect(loginButton, SIGNAL(clicked()), this, SLOT(onOkButtonClicked()));
}

void LoginViewDialog::onOkButtonClicked()
{
    /* TODO: Make the app check the input with the system. */
    if(nameEdit->text() == USERNAME && passEdit->text() == PASSWORD) {
        this->accept();
    }
    else {
        errorLabel->setText("INVALID USERNAME/PASSWORD!");
    }
}

void LoginViewDialog::initDialogComponents()
{
    BaseDialog::initDialogComponents();

    this->setMainText("Login");
    this->setSubText("Please provide your login credentials.");

    gridLayout = new QGridLayout;

    nameLabel = new QLabel;
    nameEdit = new QLineEdit;

    passLabel = new QLabel;
    passEdit = new QLineEdit;

    errorLabel = new QLabel;

    nameLabel->setText("Username:");

    passLabel->setText("Password:");
    passEdit->setEchoMode(QLineEdit::Password);

    errorLabel->setAlignment(Qt::AlignCenter);

    gridLayout->addWidget(nameLabel, 0, 0);
    gridLayout->addWidget(nameEdit, 0, 1);
    gridLayout->addWidget(passLabel, 1, 0);
    gridLayout->addWidget(passEdit, 1, 1);

    mainLayout->addLayout(gridLayout);
    mainLayout->addWidget(errorLabel);
    mainLayout->addStretch();

    mainLayout->addWidget(new SeparatorWidget);

    loginButton = new QPushButton("&Login", this);
    loginButton->setDefault(true);

    buttonBox = new QDialogButtonBox();
    buttonBox->addButton(loginButton, QDialogButtonBox::AcceptRole);
    buttonBox->setCenterButtons(true);

    mainLayout->addWidget(buttonBox);
}
